﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ECare.BUS;
using ECare.BUS.Data;
using ECare.Client.templates;

namespace ECare.Client.frms
{
    public partial class frmPlanInfo : frmInfo
    {
        public frmPlanInfo()
        {
            InitializeComponent();


            this.Load += frmPlanInfo_Load;


        }

        void frmPlanInfo_Load(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            this.LoadFormulas();

            if (this.entity != null)
            {
                var plan = this.entity as PlanInfo;
                this.cbFormula.SelectedValue = plan.formula_id;
                this.txtDate.Value = plan.plan_date;
                this.txtNumber.Value = plan.plan_times;
            }

        }

        private void LoadFormulas()
        {
            var formulas = FormulaHelper.GetFormulaInfos();
            this.cbFormula.DataSource = formulas;
            this.cbFormula.ValueMember = "FormulaId";
            this.cbFormula.DisplayMember = "FormulaName";


        }



        private void btnSave_Click(object sender, EventArgs e)
        {

            if (!ValidateDropdownlist(this.cbFormula))
                return;
         

            if (this.entity == null)
            {
                var newplan = new PlanInfo();
                newplan.formula_id = int.Parse(this.cbFormula.SelectedValue.ToString());
                newplan.plan_date = this.txtDate.Value;
                newplan.plan_times = (int)this.txtNumber.Value;

                newplan.plan_id=PlanHelper.CreatePlan(newplan);
                newplan.plan_create_by = ApplicationData.CurrentUserInfo.UserId;
                newplan.plan_create_time = DateTime.Now;

                this.entity = newplan;
            }
            else
            {
                var newplan = this.entity as PlanInfo;

                newplan.formula_id = int.Parse(this.cbFormula.SelectedValue.ToString());
                newplan.plan_date = this.txtDate.Value;
                newplan.plan_times = (int)this.txtNumber.Value;


                newplan.plan_update_by = ApplicationData.CurrentUserInfo.UserId;
                newplan.plan_update_time = DateTime.Now;
            }

            if (this.Saved != null)
                this.Saved(this.entity);

            this.Close();

        }

        public event EntitySavedHandler Saved;
    }
}
